<?php

namespace Admin\Controller;

class FinishedOrderController extends AdminbaseController {

    function _initialize() {
        parent::_initialize();
        $this->currentModel = D("FinishedOrder");
    }

    //首页
    public function index() {

        $requestMap = $this->convertRequestToMap();

        $this->assign("begin_time", $_REQUEST["o#create_time-egt"]);
        $this->assign("end_time", $_REQUEST["o#create_time-elt"]);

        //分页
        $count = $this->currentModel->getListCount($requestMap);
        $page = $this->page($count, C("DEFAULT_PAGE_SIZE"));

        //获取order列表
        $orderList = $this->currentModel->queryForList($page, $requestMap);

        $this->assign("defaultEnum", C("INVOICE_TYPE"));
        $this->assign("statusEnum", C("FINISHED_ORDER_STATUS"));
        $this->assign('orderList', $orderList);
        $this->assign("page", $page->show('Admin', $this->parameter));
        $this->display();

    }


    public function endDelivery() {

        $id = $_REQUEST["id"];
        $finishedOrder = $this->currentModel->queryById($id);

        $this->assign("finishedOrder", $finishedOrder);
        $this->display();

    }

    public function end() {


        $id = $_REQUEST["id"];

        $finishedOrder = $this->currentModel->queryById($id);

        $finishedOrder["status"] = C("FINISHED_ORDER_STATUS.END_DELIVERY");
        $finishedOrder["delivery_number"] = $_REQUEST["deliveryNumber"];
        $finishedOrder["express_company"] = $_REQUEST["expressCompany"];
        $finishedOrder["outstock_time"] = getCurrentDate();
        $finishedOrder["delivery_id"] = $_SESSION['ADMIN_ID'];

        $this->currentModel->updateInfo($finishedOrder);

        //推送留言信息到销售
        $employeeList = D("Employee")->alias("e")
            ->join("left join __CUSTOMER__ c on c.customer_id = e.employee_id")
            ->field("e.*, c.openid")
            ->select();
        $openidArray = array();
        $customer = D("Customer")->queryById($finishedOrder["employee_id"]);
        array_push($openidArray, $customer["openid"]);
        foreach ($employeeList as $singleEmployee) {
            $job = explode(',',$singleEmployee["job"]);
            if ($job[0] == "100") {//管理员
                array_push($openidArray, $singleEmployee["openid"]);
            }
        }

        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();

        $finishedProduct = D("FinishedProduct")->queryById($finishedOrder["finished_product_id"]);

        foreach ($openidArray as $openid) {
            $notifyData["openid"] = $openid;
            $notifyData["url"] = "http://xwkang.com/wechat/finishedProduct/orderList/id/" . $finishedOrder["finished_order_id"];
            $notifyData["first"] = "您的成品订单已发货。发货人：" . $user["admin_name"];
            $notifyData["keyword1"] = "[" . $finishedProduct["name"] . "], 共 " . $finishedOrder["number"] . " 件";
            $notifyData["keyword2"] = "[" . $finishedOrder["delivery_type"] . "]，客户：" . $finishedOrder["customer_name"];
            $notifyData["keyword3"] = "[" . $_REQUEST["expressCompany"] . "]" . $_REQUEST["deliveryNumber"];
            $notifyData["remark"] = "订单总额：" . $finishedOrder["total_price"] . " 元";
            finishedOrderEndNotify($notifyData);
        }

        $this->success("已标记成品订单为已出库状态", U("finishedOrder/index"));

    }

}